package com.zdl.interestingapp.utils

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Rect
import android.util.DisplayMetrics
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import org.jetbrains.annotations.NotNull


/**
 * @author zdl
 * @date 2018/4/24 8:50
 * email zdl328465042@163.com
 * description 封装与手机相关的工具
 */
object MyPhoneUtil {

    /**
     * 得到屏幕的宽
     * @param activity activity
     * @return 宽
     */
    fun getScreenWidth(activity: Activity): Int {
        val dm = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(dm)
        return dm.widthPixels
    }

    /**
     * 得到屏幕的高
     * @param activity activity
     * @return 高
     */
    fun getScreenHeight(activity: Activity): Int {
        val dm = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(dm)
        return dm.heightPixels
    }

    /**
     * 根据手机的分辨率从dp的单位转成为px(像素)
     */
    fun dip2px(res: Resources, dpValue: Int): Int {
        val scale = res.displayMetrics.density
        return (dpValue * scale + 0.5F).toInt()
    }

    /**
     * 根据手机的分辨率从px(像素)的单位转成为dp
     */
    fun px2dip(res: Resources, pxValue: Int): Int {
        val scale = res.displayMetrics.density
        return (pxValue/scale + 0.5F).toInt()
    }

    /**
     * 判断当前软键盘是否显示
     * @param activity activity
     * @return true:已显示 false:隐藏
     */
    fun isSoftInputShowing(@NotNull activity: Activity): Boolean {
        //获取当前屏幕内容的高度
        val screenHeight = activity.window.decorView.height
        //获取view可见区域的bottom
        val rect = Rect()
        activity.window.decorView.getWindowVisibleDisplayFrame(rect)

        return screenHeight - rect.bottom != 0
    }

    /**
     * 显示软键盘
     * @param activity activity
     * @param et 输入控件
     */
    fun showSoftInput(@NotNull activity: Activity, @NotNull et: EditText) {
        val view = activity.window.peekDecorView()
        if (null != view) {
            val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.showSoftInput(et, 0)
        }
    }

    /**
     * 隐藏软键盘
     * @param activity activity
     */
    fun dismissSoftInput(@NotNull activity: Activity) {
        val view = activity.window.peekDecorView()
        if (null != view) {
            val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    /**
     * 根据传入的view和event位置，判断是否应该关闭软键盘
     * @param view view
     * @param event event
     * @return true:应该关闭 false:不应该关闭
     */
    fun isShouldHideSoftInput(@NotNull view: View, @NotNull event: MotionEvent): Boolean {
        val leftTop = intArrayOf(0, 0)
        //获取输入框当前的location位置
        view.getLocationInWindow(leftTop)
        val left = leftTop[0]
        val top = leftTop[1]
        val right = left + view.width
        val bottom = top + view.height

        val x = event.x
        val y = event.y

        return x < left || x > right || y < top || y > bottom
    }

    /**
     * 获取手机状态栏高度
     * @param activity activity
     * @return 状态栏高度
     */
    fun getStatusBarHeight(@NotNull activity: Activity): Int {
        var stateBarHeight = -1
        //获取status_bar_height资源的ID
        val resourcesId = activity.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourcesId > 0){
            //根据资源ID获取响应的尺寸值
            stateBarHeight = activity.resources.getDimensionPixelSize(resourcesId)
        }
        return stateBarHeight
    }

    /**
     * 获取手机标题栏高度
     * @param activity activity
     * @return 标题栏高度
     */
    fun getTitleBarHeight(@NotNull activity: Activity): Int {
        val contentTop = activity.window.findViewById<View>(Window.ID_ANDROID_CONTENT).top
        return contentTop - getScreenHeight(activity)
    }
}